<?php
namespace app\admin\controller;

use think\Session;
use app\common\controller\AdminBase;
use app\common\model\AuthGroupAccess as AuthGroupAccessModel;
use app\common\model\AuthGroup as AuthGroupModel;
use app\common\model\AuthRule as AuthRuleModel;

class Power extends AdminBase
{
    protected $auth_group_model;
    protected $auth_rule_model;
    protected $auth_group_access_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->auth_group_access_model = new AuthGroupAccessModel();
        $this->auth_group_model = new AuthGroupModel();
        $this->auth_rule_model  = new AuthRuleModel();
    }

    public function index(){
        $auth_group_list = $this->auth_group_model->where('uid',session::get('admin_id'))->select();
        return $this->fetch('index', ['auth_group_list' => $auth_group_list]);
    }

    public function add(){
        return $this->fetch();
    }

    /**
     * 保存权限组
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['uid'] = session::get('admin_id');
            if ($this->auth_group_model->save($data) !== false) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
    }

    /**
     * 授权
     * @param $id
     * @return mixed
     */
    public function auth($id)
    {
        return $this->fetch('auth', ['id' => $id]);
    }

    /**
     * AJAX获取规则数据
     * @param $id
     * @return mixed
     */
    public function getJson($id)
    {
        $auth_group_data = $this->auth_group_model->find($id)->toArray();
        // dump($auth_group_data);
        $auth_rules      = explode(',', $auth_group_data['rules']);
        // dump($auth_rules);
        $auth_group_access = $this->auth_group_access_model->where('uid',session::get('admin_id'))->value('group_id'); 
        // dump($auth_group_access);
        $auth_group_data_data = $this->auth_group_model->find($auth_group_access)->toArray();
        // dump($auth_group_data_data);
        $auth_rule_list  = $this->auth_rule_model->whereIn('id',$auth_group_data_data['rules'])->field('id,pid,title')->select();
        // dump($auth_rule_list);
        // exit();
        foreach ($auth_rule_list as $key => $value) {
            in_array($value['id'], $auth_rules) && $auth_rule_list[$key]['checked'] = true;
        }

        return $auth_rule_list;
    }

    /**
     * 更新权限组规则
     * @param $id
     * @param $auth_rule_ids
     */
    public function updateAuthGroupRule($id, $auth_rule_ids = '')
    {
        if ($this->request->isPost()) {
            if ($id) {
                $group_data['id']    = $id;
                $group_data['rules'] = is_array($auth_rule_ids) ? implode(',', $auth_rule_ids) : '';

                if ($this->auth_group_model->save($group_data, $id) !== false) {
                    $this->success('授权成功');
                } else {
                    $this->error('授权失败');
                }
            }
        }
    }










}